<?php

/**
 * This is the model class for table "cm_details".
 *
 * The followings are the available columns in table 'cm_details':
 * @property integer $contact_id
 * @property integer $best_phone
 * @property string $address_1
 * @property string $address_2
 * @property string $birthday
 * @property string $cell_phone
 * @property string $city
 * @property string $country
 * @property string $home_phone
 * @property string $state
 * @property string $work_phone
 * @property string $zip
 */
class CmDetails extends CActiveRecord
{
	const HOME_PHONE = 1;
	const WORK_PHONE = 2;
	const CELL_PHONE = 3;
	
	public $best_phone = self::WORK_PHONE;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmDetails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cm_details';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('best_phone', 'numerical', 'integerOnly'=>true),
			array('address_1, address_2, birthday, cell_phone, city, country, home_phone, state, work_phone, zip', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('address_1, address_2, birthday, cell_phone, city, country, home_phone, state, work_phone, zip', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'contact_id' => 'Contact:',
			'best_phone' => 'Best Phone',
			'address_1' => 'Address 1:',
			'address_2' => 'Address 2:',
			'birthday' => 'Birthday:',
			'cell_phone' => 'Cell:',
			'city' => 'City:',
			'country' => 'Country:',
			'home_phone' => 'Home:',
			'state' => 'State:',
			'work_phone' => 'Work:',
			'zip' => 'Zip:',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

//		$criteria->compare('contact_id',$this->contact_id);
//		$criteria->compare('best_phone',$this->best_phone);
		$criteria->compare('address_1',$this->address_1,true);
		$criteria->compare('address_2',$this->address_2,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('cell_phone',$this->cell_phone,true);
		$criteria->compare('city',$this->city,true);
		$criteria->compare('country',$this->country,true);
		$criteria->compare('home_phone',$this->home_phone,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('work_phone',$this->work_phone,true);
		$criteria->compare('zip',$this->zip,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function explodeBirthday(){
		$date = null;
		if (isset($this->birthday)) {
			$date = explode('-', $this->birthday);
		}
		return $date;
	}
	public function getBirthDay() {
		$day = 1;
		if (is_array($date = $this->explodeBirthday())) {
			if (isset($date[2])) {
				$day = (int)$date[2];
			}
		}
		return $day;
	}
	public function getBirthMonth() {
		$month = 1;
		if (is_array($date = $this->explodeBirthday())) {
			if (isset($date[1])) {
				$month = (int)$date[1];
			}
		}
		return $month;
	}
	public function getBirthYear() {
		$year = 1980;
		if (is_array($date = $this->explodeBirthday())) {
			if (isset($date[0])) {
				$year = (int)$date[0];
			}
		}
		return $year;
	}
	public function getBestPhone(){
		$phone = '';
		switch ($this->best_phone) {
			case self::HOME_PHONE:
				$phone = $this->home_phone;
				break;
			case self::CELL_PHONE:
				$phone = $this->cell_phone;
				break;
			default:
				$phone = $this->work_phone;
				break;
		}
		return $phone;
	}
}